#include "StdAfx.h"
#include "dvisitorserializer.h"
#include "dabstractentity.h"
#include "dclassentity.h"
#include "denumentity.h"
#include "dmethodentity.h"
#include "dvariableentity.h"

DVisitorSerializer::DVisitorSerializer(const QString& strPath, bool bSave, QObject *parent)
	: DVisitor(parent)
{
	m_file.setFileName(strPath);
	m_file.open(QIODevice::WriteOnly | QIODevice::Text);
	m_writer.setDevice(&m_file);

	m_writer.writeStartDocument();
	m_writer.writeStartElement("CodeGenData");
}

DVisitorSerializer::~DVisitorSerializer()
{
	m_writer.writeEndElement();
	m_writer.writeEndDocument();
	m_file.close();
}

void DVisitorSerializer::visit(DAbstractEntity* pEntity)
{
	if(DClassEntity* pClass = qobject_cast<DClassEntity*>(pEntity))
	{
		visitClass(pClass);
	}
	else if(DEnumEntity* pEnum = qobject_cast<DEnumEntity*>(pEntity))
	{
		visitEnum(pEnum);
	}
}

void DVisitorSerializer::visitClass(DClassEntity* pClass)
{
	m_writer.writeStartElement("Class");
	m_writer.writeAttribute("Name", pClass->name());
	m_writer.writeAttribute("GUID", pClass->guid());

	// Base classes should have guid so we can bind derived classes by id and subclass them
	// To simplify things with topmost class (to avoid 'special guid' for class with no base class) 
	// we will introduce bHasBaseClass variable 

	DClassEntity::Methods methods = pClass->methods();
	DClassEntity::Variables vars = pClass->variables();

	if(methods.size())
	{

	}

	if(vars.size())
	{

	}

	m_writer.writeEndElement(); // Class
}

void DVisitorSerializer::visitEnum(DEnumEntity* pEnum)
{

}

void DVisitorSerializer::visitMethod(DMethodEntity* pMethod)
{

}

void DVisitorSerializer::visitVariable(DVariableEntity* pVar)
{

}